#include "charging_pile.h"

#include "utility.h"

ChargingPile::ChargingPile( QObject* parent ) {}

bool ChargingPile::setOutput( QTcpSocket& mSocket, ChargingTcpDrive::Operator oper ) {
    if ( !isConnected( mSocket ) ) { return false; }
    // 点动
    // uint8_t data[]{0xCC, 0xDD, 0x33, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

    uint8_t data[]{ 0xCC, 0xDD, 0xA1, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

    switch ( oper ) {
    case OPEN:
        data[ 4 ] = 0x00;
        data[ 5 ] = 0b01;
        data[ 6 ] = 0x00;
        data[ 7 ] = 0b11;
        break;
    case CLOSE:
        data[ 4 ] = 0x00;
        data[ 5 ] = 0b10;
        data[ 6 ] = 0x00;
        data[ 7 ] = 0b11;
        break;
    case STOP:
        data[ 4 ] = 0x00;
        data[ 5 ] = 0b00;
        data[ 6 ] = 0x00;
        data[ 7 ] = 0b11;
        break;
    case UNKNOWN:
        return false;
    }

    unsigned char CH = data[ 2 ] + data[ 3 ] + data[ 4 ] + data[ 5 ] + data[ 6 ] + data[ 7 ];
    unsigned char CL = 2 * CH;

    data[ 8 ] = CH;
    data[ 9 ] = CL;

    QByteArray array( ( char* )data, 10 );

    emit sigShowTCPData( array, QString( "Send: [%1:%2] [写输出] " ).arg( mSocket.peerName() ).arg( mSocket.peerPort() ) );
    mSocket.write( array, 10 );

    mSocket.waitForReadyRead( 800 );
    QByteArray feedback = mSocket.readAll();
    sigShowTCPData( feedback, QString( "Recv: [%1:%2] [写输出] " ).arg( mSocket.peerName() ).arg( mSocket.peerPort() ) );

    if ( feedback.size() < 3 ) { return false; }

    return feedback.at( 0 ) == 0x4F && feedback.at( 1 ) == 0x4B && feedback.at( 2 ) == 0x21;
}

bool ChargingPile::updateInput( QTcpSocket& mSocket ) {
    if ( !isConnected( mSocket ) ) { return false; }

    mSocket.readAll();

    uint8_t    data[]{ 0xCC, 0xDD, 0xC0, 0x01, 0x00, 0x00, 0x0D, 0xCE, 0x9C };
    QByteArray array( ( char* )data, 9 );
    emit       sigShowTCPData( array, QString( "Send: [%1:%2] [读输入] " ).arg( mSocket.peerName() ).arg( mSocket.peerPort() ) );
    mSocket.write( array, 9 );

    mSocket.waitForReadyRead( 800 );
    QByteArray feedback = mSocket.readAll();
    sigShowTCPData( feedback, QString( "Recv: [%1:%2] [读输入] " ).arg( mSocket.peerName() ).arg( mSocket.peerPort() ) );

    if ( feedback.size() < 9 ) {
        qDebug() << "ChargingPile::updateInput: size of feedback < 9, " << Utility::QbyteArry2Qstring( feedback );
        return false;
    }

    if ( ( unsigned char )feedback.at( 0 ) != 0xEE     //
         || ( unsigned char )feedback.at( 1 ) != 0xFF  //
         || ( unsigned char )feedback.at( 2 ) != 0xC0  //
         || ( unsigned char )feedback.at( 3 ) != 0x01  //
         || ( unsigned char )feedback.at( 6 ) != 0x0D ) {
        qDebug() << "ChargingPile::updateInput: data unmatched, " << Utility::QbyteArry2Qstring( feedback );
        return false;
    }

    unsigned char CH = feedback.at( 2 ) + feedback.at( 3 ) + feedback.at( 4 ) + feedback.at( 5 ) + feedback.at( 6 );
    unsigned char CL = 2 * CH;

    if ( ( unsigned char )feedback.at( 7 ) != CH || ( unsigned char )feedback.at( 8 ) != CL ) {
        qDebug() << "ChargingPile::updateInput: CH or CL unmatched, " << Utility::QbyteArry2Qstring( feedback );
        return false;
    }

    limits.back  = ( feedback.at( 5 ) & 0b01 ) > 0;
    limits.front = ( feedback.at( 5 ) & 0b10 ) > 0;

    return true;
}

bool ChargingPile::updateOutput( QTcpSocket& mSocket ) {
    if ( !isConnected( mSocket ) ) { return false; }

    mSocket.readAll();

    uint8_t    data[]{ 0xCC, 0xDD, 0xB0, 0x01, 0x00, 0x00, 0x0D, 0xBE, 0x7C };
    QByteArray array( ( char* )data, 9 );
    emit       sigShowTCPData( array, QString( "Send: [%1:%2] [读输出] " ).arg( mSocket.peerName() ).arg( mSocket.peerPort() ) );
    mSocket.write( array, 9 );

    mSocket.waitForReadyRead( 800 );
    QByteArray feedback = mSocket.readAll();

    sigShowTCPData( feedback, QString( "Recv: [%1:%2] [读输出] " ).arg( mSocket.peerName() ).arg( mSocket.peerPort() ) );

    if ( feedback.size() < 8 ) {
        qDebug() << "ChargingPile::updateOutput: size of feedback < 8, " << Utility::QbyteArry2Qstring( feedback );
        return false;
    }

    if ( ( unsigned char )feedback.at( 0 ) != 0xAA     //
         || ( unsigned char )feedback.at( 1 ) != 0xBB  //
         || ( unsigned char )feedback.at( 2 ) != 0xB0  //
         || ( unsigned char )feedback.at( 3 ) != 0x01  //
         || ( unsigned char )feedback.at( 6 ) != 0x0D ) {
        qDebug() << "ChargingPile::updateOutput: data unmatched, " << Utility::QbyteArry2Qstring( feedback );
        return false;
    }

    unsigned char CH = feedback.at( 2 ) + feedback.at( 3 ) + feedback.at( 4 ) + feedback.at( 5 ) + feedback.at( 6 );
    //  unsigned char CL = 2 * CH;

    if ( ( unsigned char )feedback.at( 7 ) != CH /* || (unsigned char)feedback.at(8) != CL*/ ) {
        qDebug() << "ChargingPile::updateOutput: CH or CL unmatched, " << Utility::QbyteArry2Qstring( feedback );
        return false;
    }

    switch ( feedback.at( 5 ) ) {
    case 0x00:
        output = STOP;
        break;
    case 0x01:
        output = OPEN;
        break;
    case 0x02:
        output = CLOSE;
        break;
    case 0x03:
        output = UNKNOWN;
        break;
    }

    return true;
}
